{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Random response generator\n", "\n", "This sheet will generate random systems and show their step responses. See if you can predict the responses from the transfer functions and the poles and zeros." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook assumes version 0.8.0 of the control library or better" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import control\n", "import numpy\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def viz(order):\n", " # Not all random transfer functions work, so we generate one\n", " # and try to calculate the step response, only continuing when it works\n", " valid = False\n", " while not valid:\n", " coeffs = (numpy.random.random(order)*3).tolist() + [1]\n", " G = control.tf(1, coeffs)\n", "\n", " try:\n", " t, y = control.step_response(G)\n", " valid = True\n", " except ValueError:\n", " continue\n", "\n", " fig, [ax_complex, ax_time] = plt.subplots(1, 2, figsize=(10, 5))\n", " \n", " plt.sca(ax_complex)\n", " control.pzmap(G)\n", " ax_complex.axis('equal')\n", " ax_complex.axis([-5, 5, -5, 5])\n", " ax_complex.grid()\n", " ax_time.plot(t, y)\n", " ax_time.axhline(1)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from ipywidgets import interact" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "c20086f3ed33484daddc0b03467c10dc", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(IntSlider(value=3, description='order', max=5, min=1), Output()), _dom_classes=('widget-…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "interact(viz, order=(1, 5))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }